package com.crm.controller;


import com.crm.DTO.ClueQueryDTO;

import com.crm.DTO.FishCluesDTO;
import com.crm.annotation.BusinessType;
import com.crm.annotation.Log;
import com.crm.common.result.ResultVO;
import com.crm.common.result.TableDataInfo;
import com.crm.entity.Clue;
import com.crm.service.ClueService;
import com.crm.vo.CluePageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * \* User: chy
 * \* Date: 2025/9/4  下午1:21
 * \* Description:
 * \
 */
@RestController
@RequestMapping("/clues/clue")
@Slf4j
public class ClueController {

    @Autowired
    private ClueService clueService;

    /**
     * 线索列表
     * @param queryDTO
     * @return
     */
    @RequestMapping("/list")
    public TableDataInfo<Clue> list(ClueQueryDTO queryDTO,
                                          @RequestParam(value = "params[beginCreateTime]", required = false) LocalDate beginCreateTime,
                                          @RequestParam(value = "params[endCreateTime]", required = false) LocalDate endCreateTime) {

        if(beginCreateTime != null && endCreateTime != null){
            queryDTO.setBeginCreateTime(beginCreateTime.atStartOfDay());
            queryDTO.setEndCreateTime(endCreateTime.atStartOfDay());
        }
        TableDataInfo<Clue> cluePage = clueService.pageCules(queryDTO);
        return cluePage;
    }

    /**
     * 保存线索信息
     * @param clue
     * @return
     */
    @PostMapping
    @Log(title = "线索管理", businessType = BusinessType.INSERT)
    public ResultVO saveClue(@RequestBody Clue clue) {
        log.info("保存线索信息：{}", clue);

        clueService.saveClue(clue);
        return  ResultVO.success();
    }

    /**
     * 根据id查询线索信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResultVO<Clue> getClueById(@PathVariable Long id) {
        log.info("查询线索信息：{}", id);

        Clue clue = clueService.getById(id);
        return ResultVO.success(clue);
    }

    /**
     * 转伪线索
     * @param id
     * @param clue
     * @return
     */
    @PutMapping("/false/{id}")
    @Log(title = "线索管理", businessType = BusinessType.UPDATE)
    public ResultVO updateClue(@PathVariable Long id, @RequestBody Clue clue) {
        log.info("修改线索信息：{}", clue);

        boolean update = clueService.updatePseudoById(id ,clue);
        return update ? ResultVO.success() : ResultVO.error();
    }

    /**
     * 线索池分页查询
     * @param queryDTO
     * @return
     */
    @GetMapping("/pool")
    public TableDataInfo<Clue>  getCluePool(ClueQueryDTO queryDTO,
                                                  @RequestParam(value = "params[beginCreateTime]", required = false) LocalDate beginCreateTime,
                                                  @RequestParam(value = "params[endCreateTime]", required = false) LocalDate endCreateTime) {
        log.info("查询线索池{}", queryDTO);

        if(beginCreateTime != null && endCreateTime != null){
            queryDTO.setBeginCreateTime(beginCreateTime.atStartOfDay());
            queryDTO.setEndCreateTime(endCreateTime.atStartOfDay());
        }
        TableDataInfo<Clue>  cluePool = clueService.getCluePool( queryDTO);
        return cluePool;
    }

    /**
     * 线索池批量捞取
     * @param fishCluesDTO
     * @return
     */
    @PutMapping("/gain")
    @Log(title = "线索管理", businessType = BusinessType.UPDATE)
    public ResultVO gainClue(@RequestBody FishCluesDTO fishCluesDTO) {
        log.info("线索池领取线索{}", fishCluesDTO);

        boolean update = clueService.updateFishWithById(fishCluesDTO);
        return update ? ResultVO.success() : ResultVO.error();
    }

    /**
     * 线索转换为商机
     * @param id
     * @return
     */
    @PutMapping("/changeBusiness/{id}")
    @Log(title = "线索管理", businessType = BusinessType.UPDATE)
    public ResultVO changeBusiness(@PathVariable Long id) {
        log.info("线索池领取线索{}", id);
        boolean update = clueService.changeBusiness(id);
        return update ? ResultVO.success() : ResultVO.error();
    }

}
